﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MedicalLog.Domain.Entities;

namespace MedicalLog.Domain.Repositories
{
    public class TutorRepository
    {
        MedicalLogEntities _dbMedicalLog;

        public TutorRepository()
        {
            _dbMedicalLog = new MedicalLogEntities();

        }

        /// <summary>
        /// This method allows you to add a tutor in the database
        /// </summary>
        /// <param name="newTutor"></param>
        public void AddTutor(Tutor newTutor, Patient patient)
        {
            var getPatient = (from p in _dbMedicalLog.Patients
                              where (patient.Telephone == p.Telephone) 
                              && (patient.Name == p.Name)
                              select p.Id);

            newTutor.IdPatient = getPatient.First();
            _dbMedicalLog.AddToTutors(newTutor);
            _dbMedicalLog.SaveChanges();
        }

        /// <summary>
        /// This method allows you to delete a tutor in the database
        /// </summary>
        /// <param name="Id"></param>
        public void DeleteTutor(int Id)
        {
            var tutor = _dbMedicalLog.Tutors.Where(x => x.IdTutor == Id).First();
            if (tutor != null)
            {
                _dbMedicalLog.Tutors.DeleteObject(tutor);
                _dbMedicalLog.SaveChanges();
            }
        }
        /// <summary>
        /// This method allows you to view the detail of a tutor
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Tutor DetailTutor(int Id)
        {
            var Tutor = _dbMedicalLog.Tutors.First(x => x.IdTutor == Id);
            return Tutor;
        }

        /// <summary>
        /// This method allows you to update a tutor
        /// </summary>
        /// <param name="oldTutor"></param>
        public void UpdateTutor(Tutor oldTutor)
        {
            _dbMedicalLog.Tutors.FirstOrDefault(x => x.IdTutor == oldTutor.IdTutor);
            _dbMedicalLog.Tutors.ApplyCurrentValues(oldTutor);
            _dbMedicalLog.SaveChanges();
        }

        /// <summary>
        /// This method allows you to search a tutor.
        /// </summary>
        /// <param name="pId"></param>
        /// <returns></returns>
        public Tutor SearchTutor(string pId)
        {
            var tutor = _dbMedicalLog.Tutors.First(x => x.IdentificationNumber == pId);
            return tutor;
        }
    }
}
